package com.ly.cloud.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.ly.cloud.dto.StuFamilyInfoDto;
import com.ly.cloud.entity.StuFamilyInfoPo;
import com.ly.cloud.exception.CloudException;
import com.ly.cloud.exception.mapper.DBException;
import com.ly.cloud.mapper.StuFamilyInfoMapper;
import com.ly.cloud.service.StuFamilyInfoService;
import com.ly.cloud.utils.BeanCopyUtils;
import com.ly.cloud.vo.StuFamilyInfoVo;

@Service
@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
public class StuFamilyInfoServiceImpl implements StuFamilyInfoService {

	@Autowired
	private StuFamilyInfoMapper stuFamilyInfoMapper;
	/**(non-Javadoc)
	 * Title: insertStudentInfo
	 * Description: 
	 * 添加学生信息
	 * @param dto 条件封装对象
	 * @param cjr 创建人
	 * @return  boolean
	 * @date: 2018年12月21日
	 * @author luhuacong
	 */
	@Override
	public boolean insertStuFamilyInfo(StuFamilyInfoDto dto, String cjr) {
		StuFamilyInfoPo po = BeanCopyUtils.copyBeanProperties(dto, StuFamilyInfoPo.class);
		//设置创建人
		po.setCjr(cjr);
		//设置创建时间
		po.setCjsj(new Date());
		Integer i = 0;
		 try {
			i = stuFamilyInfoMapper.insert(po);
			if(i>0) {
				return true;
			}else {
				return false;
			}
		} catch (Exception e) {
			throw new DBException("添加学生家庭成员信息失败！", e);
		}
	}
	
	/**(non-Javadoc)
	 * Title: deleteById
	 * Description: 
	 * 根据主键删除成员信息
	 * @param cyzj 成员主键
	 * @return  boolean
	 * @date: 2018年12月22日
	 * @author luhuacong
	 */
	@Override
	public boolean deleteById(Long cyzj) {
		Integer i = 0;
		try {
			i = stuFamilyInfoMapper.deleteById(cyzj);
			if(i>0) {
				return true;
			}else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new DBException("删除学生家庭成员信息失败！", e);
		}
	}

	/** 
	 * Method Name:deleteFamliyList
	 * Description:批量删除家庭成员信息
	 * @param idList 主键集合
	 * @return 成功删除的个数
	 * @author LuHuaCong
	 * @date: 2018年12月22日
	 */
	@Override
	public Integer deleteFamliyList(List<Long> idList) {
		Integer count = 0;		
		try {			
			count = stuFamilyInfoMapper.deleteBatchIds(idList);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DBException("批量删除学生家庭成员信息失败！", e);
		}
		return count;
	}

	/**(non-Javadoc)
	 * Title: updateStuFamilyInfo
	 * Description: 
	 * 修改学生家庭成员信息
	 * @param dto 条件封装对象
	 * @param xgr 修改人
	 * @return  boolean
	 * @date: 2018年12月22日
	 * @author luhuacong
	 */
	@Override
	public boolean updateStuFamilyInfo(StuFamilyInfoDto dto, String xgr) {
		StuFamilyInfoPo po = BeanCopyUtils.copyBeanProperties(dto, StuFamilyInfoPo.class);
		//设置修改人
		po.setXgr(xgr);
		//设置创建时间
		po.setXgsj(new Date());
		
		
		Integer i = 0;
		 try {
			i = stuFamilyInfoMapper.updateById(po);
			if(i>0) {
				return true;
			}else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new DBException("修改学生家庭成员信息失败！", e);
		}
	}

	/**(non-Javadoc)
	 * Title: selectFamListByxsid
	 * Description: 
	 * 根据学生id查询家庭成员信息
	 * @page 
	 * @param xsid 学生id
	 * @return  PageInfo<StuFamilyInfoVo>
	 * @date: 2018年12月22日
	 * @author luhuacong
	 */
	@Override
	public Page<StuFamilyInfoVo> selectFamListByxsid(Page<StuFamilyInfoVo> page,String xsid) {
		List<StuFamilyInfoVo> voList = null;
		try {
			voList = stuFamilyInfoMapper.selectFamListByxsid(page, xsid);
			page.setRecords(voList);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CloudException("查询学生家庭成员列表失败", e);
		}
		return page;
	}	
}
